﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web;

namespace Forest.Web.Framework.Mvc.Routes {
    public class GuidConstraint : IRouteConstraint {
        private readonly bool _allowEmpty;

        public GuidConstraint(bool allowEmpty) {
            this._allowEmpty = allowEmpty;
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
            if (values.ContainsKey(parameterName)) {
                string stringValue = values[parameterName] as string;

                if (!string.IsNullOrEmpty(stringValue)) {
                    Guid guidValue;

                    return Guid.TryParse(stringValue, out guidValue) &&
                        (_allowEmpty || guidValue != Guid.Empty);
                }
            }

            return false;
        }
    }
}
